const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin');

// const isProd = process.env.NODE_ENV.startsWith('prod')
console.log(11111, process.env.NODE_ENV)
const isProd = process.env.NODE_ENV === 'production'
const externals = {
  'element-ui': 'ELEMENT',
  vue: 'Vue',
  vuex: 'Vuex',
  'vue-router': 'VueRouter',
  axios: 'axios',
  // echarts: 'echarts'
}

const cdn = {
  css: [
    'https://cdn.bootcdn.net/ajax/libs/element-ui/2.15.9/theme-chalk/index.min.css',
  ],
  js: [
    'https://cdn.bootcdn.net/ajax/libs/vue/2.6.11/vue.min.js',
    'https://cdn.bootcdn.net/ajax/libs/element-ui/2.15.9/index.js',
    'https://cdn.bootcdn.net/ajax/libs/axios/0.27.2/axios.min.js',
    'https://cdn.bootcdn.net/ajax/libs/vuex/3.5.1/vuex.min.js',
    'https://cdn.bootcdn.net/ajax/libs/vue-router/3.2.0/vue-router.min.js',
    // 'https://cdn.staticfile.org/echarts/4.3.0/echarts.min.js',
  ]
}

module.exports = {
  publicPath: '/vue/', // 默认为'/'

  // 将构建好的文件输出到哪里，本司要求
  outputDir: 'dist',

  // 放置生成的静态资源(js、css、img、fonts)的目录。
  assetsDir: 'static',

  runtimeCompiler: true,
  // 是否在开发环境下通过 eslint-loader 在每次保存时 lint 代码 (在生产构建时禁用 eslint-loader)
  lintOnSave: process.env.NODE_ENV !== 'production',

  chainWebpack: config => {
    // 移除 preload(预载) 插件
    config.plugins.delete('preload')
    // 移除 prefetch(预取) 插件
    config.plugins.delete('prefetch')

    config.when(isProd, config => {
      config.entry('app').clear().add('./src/main-prod.js')
      config.set('externals', externals)
      config.plugin('html').tap(args => {
        args[0].cdn = cdn
        return args
      })
    })

    config.when(!isProd, config => {
      config.entry('app').clear().add('./src/main-dev.js')
    })


    // 清空默认svg规则
    config.module
      .rule('svg')
      .uses.clear()

    config.module.optimization = {
      splitChunks: {
        chunks: "all",
        maxSize: 3000,
        cacheGroups: {
          vendor: {
            test: /[\\/]node_modules[\\/]/,
            name: "vendor",
            chunks: "all",
            minSize: 2000
          }
        }
      }
    }

    config.module //针对svg文件添加svg-sprite-loader规则
      .rule('svg1')
      .test(/\.svg$/)
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end()
  },
  configureWebpack: {
    plugins: [
      new MonacoWebpackPlugin()
    ]
  },

  devServer: {
    // host: '127.0.0.1',
    // port: 8081,
    hot: true,
    disableHostCheck: true,
    overlay: { wraning: false, errors: true },
    proxy: {
      '/api': {
        target: 'http://localhost:3000',
        changeOrigin: true
      }
    }
  }
}